package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
	"vehicle-data-mgr/cache"
	"vehicle-data-mgr/models"
)

func CarAdd(c *gin.Context) {
	car_name := c.PostForm("car_name")
	price, _ := strconv.Atoi(c.PostForm("price"))
	mileage, _ := strconv.Atoi(c.PostForm("mileage"))
	data := models.Car{
		CarName: car_name,
		Price:   price,
		Mileage: mileage,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}

func CarTypeAdd(c *gin.Context) {
	kind := c.PostForm("kind")
	data := models.CarType{
		Kind: kind,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}

func CarList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	if size > 50 {
		size = 50
	}
	var car []models.CarTypeList
	/*
		SELECT
		  cars.id AS CarID,
		  car_types.kind AS Kind,
		  cars.car_name AS CarName,
		  cars.price AS Price,
		  cars.mileage AS Mileage
		FROM
		  cars
		  LEFT JOIN car_types ON car_types.id = cars.type_id
	*/
	redis_key := "car_list_" + strconv.Itoa(page)
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("cars").Select(
			"cars.id AS CarID",
			"car_types.kind AS Kind",
			"cars.car_name AS CarName",
			"cars.price AS Price",
			"cars.mileage AS Mileage",
		).
			Joins("LEFT JOIN car_types ON car_types.id = cars.type_id").
			Debug().Offset(offset).Limit(size).Find(&car)
		jsonData, _ := json.Marshal(car)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &car)
	}
	var count int64
	models.DB.Model(&models.Car{}).Count(&count)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "列表获取成功",
		"data": gin.H{
			"car":   car,
			"count": count,
		},
	})
}

func CarDetail(c *gin.Context) {
	id := c.Query("id")
	var car models.CarTypeList
	/*
		SELECT
		  cars.id AS CarID,
		  car_types.kind AS Kind,
		  cars.car_name AS CarName,
		  cars.price AS Price,
		  cars.mileage AS Mileage
		FROM
		  cars
		  LEFT JOIN car_types ON car_types.id = cars.type_id
	*/
	redis_key := "car_detail_" + id
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("cars").Select(
			"cars.id AS CarID",
			"car_types.kind AS Kind",
			"cars.car_name AS CarName",
			"cars.price AS Price",
			"cars.mileage AS Mileage",
		).
			Joins("LEFT JOIN car_types ON car_types.id = cars.type_id").
			Where("cars.id = ?", id).
			Debug().Find(&car)
		jsonData, _ := json.Marshal(car)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &car)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "详情获取成功",
		"data": gin.H{
			"car": car,
		},
	})
}
